<template>
  <!-- 工艺参数监控规则明细表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="70%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="参数描述" prop="parameterDesc">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.parameterDesc"
              placeholder="请输入参数描述"
              clearable
            />

            <span v-else>{{ formData.parameterDesc }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工艺数据类别" prop="processDataType">
            <el-select
              style="width: 100%"
              v-if="!props.isReadonly"
              v-model="formData.processDataType"
              placeholder="请选择工艺数据类别"
              clearable
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList(
                  'processDataType'
                )"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <span v-else>{{
              dictionaryStore.filterDictName(
                "processDataType",
                formData.processDataType
              )
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="参数值" prop="parameterValue">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.parameterValue"
              placeholder="请输入参数值"
              clearable
            />

            <span v-else>{{ formData.parameterValue }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="控制范围下限值" prop="lowerLimitValue">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.lowerLimitValue"
              placeholder="请输入控制范围下限值"
              clearable
            />

            <span v-else>{{ formData.lowerLimitValue }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="控制范围上限值" prop="upperLimitValue">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.upperLimitValue"
              placeholder="请输入控制范围上限值"
              clearable
            />

            <span v-else>{{ formData.upperLimitValue }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采样频度" prop="samplingFrequency">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.samplingFrequency"
              placeholder="请输入采样频度"
              clearable
            />

            <span v-else>{{ formData.samplingFrequency }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否生成工单" prop="isGenerateWorkOrder">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.isGenerateWorkOrder"
              placeholder="请输入是否生成工单"
              clearable
            />
            <span v-else>{{ formData.isGenerateWorkOrder }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="第一负责人名" prop="responsiblePersonName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.responsiblePersonName"
              placeholder="请输入第一负责人名"
              clearable
            />

            <span v-else>{{ formData.responsiblePersonName }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "PCMPSMPARAMMONITORDETAILADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  parameterDesc: [],
  processDataType: [],
  parameterValue: [],
  lowerLimitValue: [],
  upperLimitValue: [],
  samplingFrequency: [],
  isGenerateWorkOrder: [],
  responsiblePersonName: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  ruleId: "",
  parameterId: "",
  parameterName: "",
  parameterDesc: "",
  processDataType: "",
  parameterValue: "",
  lowerLimitValue: "",
  upperLimitValue: "",
  samplingFrequency: "",
  isGenerateWorkOrder: "",
  responsiblePersonId: "",
  responsiblePersonName: "",
  enabled: "",
  deleted: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        ruleId,
        parameterId,
        parameterName,
        parameterDesc,
        processDataType,
        parameterValue,
        lowerLimitValue,
        upperLimitValue,
        samplingFrequency,
        isGenerateWorkOrder,
        responsiblePersonId,
        responsiblePersonName,
        enabled,
        deleted,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        ruleId,
        parameterId,
        parameterName,
        parameterDesc,
        processDataType,
        parameterValue,
        lowerLimitValue,
        upperLimitValue,
        samplingFrequency,
        isGenerateWorkOrder,
        responsiblePersonId,
        responsiblePersonName,
        enabled,
        deleted,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.ruleId = "";
  formData.parameterId = "";
  formData.parameterName = "";
  formData.parameterDesc = "";
  formData.processDataType = "";
  formData.parameterValue = "";
  formData.lowerLimitValue = "";
  formData.upperLimitValue = "";
  formData.samplingFrequency = "";
  formData.isGenerateWorkOrder = "";
  formData.responsiblePersonId = "";
  formData.responsiblePersonName = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 工艺参数监控规则明细保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/dmpDosPcm/psmparammonitordetail/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
